<template>
	<view :data-theme="theme">
		<!-- 引用组件 -->
		<mangeInfoPage ref="chilrenRef" />

		<!-- 底部 -->
		<view class="footer">
			<!-- <u-button class="bg_color" :color="bgColor" @click="submitClick">保存更新</u-button> -->
			<button class="bg_color"
				style="font-weight: bold;border-radius: 16rpx;color: #FFF;padding: 20rpx;margin: 10rpx;"
				:color="bgColor" @click="submitClick">保存更新</button>
		</view>
	</view>
</template>

<script>
	
	// import mangeInfoPage from '../../components/mange-info/mange-info.vue'
	import mangeInfoPage from './mange-info/mange-info.vue'
	import {
		setThemeColor
	} from '../../utils/setTheme';
	import {
		updateManageInfoApi,
		selectManageInfoApi
	} from '@/api/merchant.js';
	let app = getApp();
	export default {
		components: {
			mangeInfoPage,
		},
		data() {
			return {
				bgColor: "#ffffff",
				theme: app.globalData.theme,
				businessInfo: {},
			};
		},
		onLoad() {
			this.bgColor = setThemeColor()
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
			// 回显数据
			this.$nextTick(async () => {
				await this.$refs.chilrenRef.selectManageInfo(1)
			})
		},
		onShow() {
			console.log("show")

		},
		methods: {
			// 保存更新
			async submitClick() {
				this.businessInfo = this.$refs.chilrenRef.businessInfo
				// 没有仓库信息
				if (!this.businessInfo.ownDepot) {
					delete this.businessInfo.depotAddress //仓库地址
					delete this.businessInfo.depotCoordinate //仓库地址经纬度
				}
				// 没有冷库地址
				if (!this.businessInfo.haveRefrigerator) {
					delete this.businessInfo.refrigeratorCoordinate //冷库地址经纬度
					delete this.businessInfo.refrigeratorAddress //冷库地址
				}
				await this.updateManageInfo()
			},
			// 添加经营信息
			async updateManageInfo() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				try {
					let result = await updateManageInfoApi(this.businessInfo)
					uni.hideLoading()
					uni.showToast({
						title: '更新成功',
						icon: 'success',
						mask: true
					})
				} catch (e) {
					uni.hideLoading()
					//TODO handle the exception
					console.log(e)
					this.$util.Tips({
						title: e
					})
				}
			},

		}
	}
</script>

<style lang="scss">
	.bg_color {
		@include main_bg_color(theme);
	}

	.footer {
		position: fixed;
		bottom: 0px;
		left: 0px;
		margin-top: 10px;
		width: 100%;
		background-color: #fff;
	}
</style>